#-*- encoding:UTF-8 -*-
import socket
import thread

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# singleton class
class seq_socket():
    __metaclass__ = Singleton

    def debug_output(self):
        print 'config - %s:%s' % (self.host_ip, self.host_port)

    def set_config(self, host_ip, host_port):
        self.host_ip = host_ip
        self.host_port = host_port

    def __send_async(self, data, callback):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto(data, (self.host_ip, self.host_port))
        rcv = s.recv(1024)
        callback(rcv)
        s.close()
        thread.exit_thread()

    def send(self, data, callback):
        thread.start_new_thread(self.__send_async, (data, callback))

'''
def callback_example(rcv):
    print rcv
b = seq_socket()
b.set_config('localhost', 17800)
b.send('data_to_send', callback_example)
b.send('data_to_send 2', callback_example)
'''
